Shader "Custom/NewSurfaceShader"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
        _modNum ("条纹数量", Range(2,30)) = 10.0
        _uRatio ("占比", Range(0,1)) = 0.3

    }
    SubShader
    {
        Tags { "RenderType"="transparent"}
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        half _modNum;
        half _uRatio;
        fixed4 _Color;
        float sdCircle(float2 p,float r){
            return length(p)-r;
        }
        // https://iquilezles.org/articles/distfunctions2d/
        float sdBox(float2 p, float2 b )
        {
            float2 d = abs(p)-b;
            return length(max(d,0.0)) + min(max(d.x,d.y),0.0);
        }
        float sdStar5(float2 p, float r, float rf)
        {
            float2 k1 = fixed2(0.809016994375, -0.587785252292);
            float2 k2 = fixed2(-k1.x,k1.y);
            p.x = abs(p.x);
            p -= 2.0*max(dot(k1,p),0.0)*k1;
            p -= 2.0*max(dot(k2,p),0.0)*k2;
            p.x = abs(p.x);
            p.y -= r;
            float2 ba = rf*fixed2(-k1.y,k1.x) - fixed2(0,1);
            float h = clamp( dot(p,ba)/dot(ba,ba), 0.0, r );
            return length(p-ba*h) * sign(p.y*ba.x-p.x*ba.y);
        }

        float sdHexagon(float2 p,float r){
            float3 k = fixed3(-0.86,0.5,0.57);
            p = abs(p);
            p-=2.0*min(dot(k.xy,p),0.0)*k.xy;
            p-=fixed2(clamp(p.x,-k.z*r,k.z*r),r);
            return length(p)*sign(p.y);
        }
        // 倒圆角 
        float opRound(float sdf,float r){
            return sdf-r;
        }
// 圆环
        float opAnnular(float sdf,float r){
            return abs(sdf)-r;
        }
        float3x3 move2d(float2 p){
            return float3x3(
                1,0,-p.x,
                0,1,-p.y,
                0,0,1
            );
        }
        float3x3 rotate2d(float r){
            float c = cos(r);
            float s = sin(r);
            return float3x3(
                c,-s,0,
                s,c,0,
                0,0,1
            );
        }
        float3x3 scale2d(float2 s){
            return float3x3(
             1/s.x,0,0,
             0,1/s.y,0,
             0,0,1
            );
        }
        
        UNITY_INSTANCING_BUFFER_START(Props)
        UNITY_INSTANCING_BUFFER_END(Props)

       

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
       
           
            // https://learn.u3d.cn/tutorial/mathematical-visualization-art?chapterId=63f44580fe40ab001f923ced#63f44a029e22c9001fb093a4
            float scale =5.0;
            
            IN.uv_MainTex = (IN.uv_MainTex*2.0-1.0) *scale;
            //  距离场
            //  坐标轴
            float axis =  smoothstep(0.01,0.0,abs(IN.uv_MainTex.y/scale))
                +smoothstep(0.01,0.0,abs(IN.uv_MainTex.x/scale)); //x=0的函数和y=0的函数，uv到-5到+5之间
            // 极坐标系
            float r = length(IN.uv_MainTex);
            float angle = atan2(IN.uv_MainTex.y,IN.uv_MainTex.x);

            float step = 0.01;
            // float circle =  smoothstep(step,-step,sdCircle(IN.uv_MainTex-float2(1,0.5),0.5));
            float sdfbox = sdBox(IN.uv_MainTex-float2(0.1,-2.5),float2(1,0.5));
            // float rect =  smoothstep(step,-step,opRound(sdfbox,0.2));
            float rect =  smoothstep(step,-step,opAnnular(sdfbox,0.06));
            float star5 =  smoothstep(step,-step,sdStar5(IN.uv_MainTex-float2(-1,0),0.8,0.3));
          
            float hex =  smoothstep(step,-step,sdHexagon(IN.uv_MainTex-float2(1,3),0.8));

            // 平移
            float3 pos2 = mul(move2d(float2(0.1,0.5)),fixed3(IN.uv_MainTex,1));
            float t = _Time.y;
            float3 pos = mul(move2d(float2(cos(t),sin(t))*2.0),fixed3(IN.uv_MainTex,1));
            // 旋转
            pos= mul(rotate2d(cos(t)*5.0),pos);
            // 缩放
            pos = mul(scale2d(float2(cos(t),cos(t))),pos);
       
// 中国国旗
            float circle =  smoothstep(step,-step,sdCircle(pos.xy,0.5));
            float rect2 = smoothstep(step,-step,sdBox(pos.xy,float2(0.5,0.5)));


            float final=circle+star5+rect+hex+rect2; //并集 

            o.Albedo = axis*_Color + final ;
            o.Alpha = 1.0;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
